{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Bucket sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bucket_sort(A, min_value, max_value):\n",
    "    buckets = [[] for _ in range(min_value, max_value+1)]\n",
    "    for x in A:\n",
    "        buckets[x].append(x)\n",
    "    sorted_arr = []\n",
    "    for bucket in buckets:\n",
    "        sorted_arr = sorted_arr + bucket\n",
    "    return sorted_arr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "arr = [5,1,2,7,3,9,4,0,6,8]\n",
    "sorted_arr = bucket_sort(arr, 0, 9)\n",
    "print(sorted_arr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Radix sort"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getDigits(x, base):\n",
    "    digits = []\n",
    "    while x > 0:\n",
    "        digits.append( x % base )\n",
    "        x = (x / base).__trunc__()\n",
    "    return digits\n",
    "        \n",
    "\n",
    "class myInt:\n",
    "    def __init__(self,x, base=10, keyDigit=0):\n",
    "        self.digits = getDigits(x, base)\n",
    "        self.keyDigit = keyDigit\n",
    "        self.value = x\n",
    "        \n",
    "    def key(self):\n",
    "        if len(self.digits) > self.keyDigit:\n",
    "            return self.digits[self.keyDigit]\n",
    "        return 0\n",
    "    \n",
    "    def updateKeyDigit(self,p):\n",
    "        self.keyDigit = p\n",
    "        \n",
    "    def getValue(self):\n",
    "        return self.value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bucketSortBase(A, base):\n",
    "    buckets = [[] for _ in range(base)]\n",
    "    for x in A:\n",
    "        buckets[x.key()].append(x)\n",
    "    sorted_arr = []\n",
    "    for bucket in buckets:\n",
    "        sorted_arr = sorted_arr + bucket\n",
    "    return sorted_arr\n",
    "\n",
    "def radix_sort(A, n_digits, base):\n",
    "    B = [ myInt(x, base=base) for x in A ]\n",
    "    for j in range(n_digits):\n",
    "        for x in B:\n",
    "            x.updateKeyDigit(j)\n",
    "        B = bucketSortBase(B, base)\n",
    "    B = [x.getValue() for x in B]\n",
    "    return B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 12, 33, 123, 523]\n"
     ]
    }
   ],
   "source": [
    "arr = [523,123,4,33,12]\n",
    "sorted_arr = radix_sort(arr, 3, 10)\n",
    "print(sorted_arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[4, 12, 33, 123, 523]\n"
     ]
    }
   ],
   "source": [
    "def bucket_sort(A, min_value, max_value):\n",
    "    buckets = [[] for _ in range(min_value, max_value+1)]\n",
    "    for x in A:\n",
    "        buckets[x].append(x)\n",
    "    sorted_arr = []\n",
    "    for bucket in buckets:\n",
    "        sorted_arr = sorted_arr + bucket\n",
    "    return sorted_arr\n",
    "\n",
    "def getDigits(x, base):\n",
    "    digits = []\n",
    "    while x > 0:\n",
    "        digits.append( x % base )\n",
    "        x = (x / base).__trunc__()\n",
    "    return digits\n",
    "        \n",
    "\n",
    "class myInt:\n",
    "    def __init__(self,x, base=10, keyDigit=0):\n",
    "        self.digits = getDigits(x, base)\n",
    "        self.keyDigit = keyDigit\n",
    "        self.value = x\n",
    "        \n",
    "    def key(self):\n",
    "        if len(self.digits) > self.keyDigit:\n",
    "            return self.digits[self.keyDigit]\n",
    "        return 0\n",
    "    \n",
    "    def updateKeyDigit(self,p):\n",
    "        self.keyDigit = p\n",
    "        \n",
    "    def getValue(self):\n",
    "        return self.value\n",
    "    \n",
    "def bucketSortBase(A, base):\n",
    "    buckets = [[] for _ in range(base)]\n",
    "    for x in A:\n",
    "        buckets[x.key()].append(x)\n",
    "    sorted_arr = []\n",
    "    for bucket in buckets:\n",
    "        sorted_arr = sorted_arr + bucket\n",
    "    return sorted_arr\n",
    "\n",
    "def radix_sort(A, n_digits, base):\n",
    "    B = [ myInt(x, base=base) for x in A ]\n",
    "    for j in range(n_digits):\n",
    "        for x in B:\n",
    "            x.updateKeyDigit(j)\n",
    "        B = bucketSortBase(B, base)\n",
    "    B = [x.getValue() for x in B]\n",
    "    return B\n",
    "\n",
    "arr = [523,123,4,33,12]\n",
    "sorted_arr = radix_sort(arr, 3, 10)\n",
    "print(sorted_arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
